<?php
class statsController extends AController {
	public function work_statsAction() {
		$page = $this->request->getGetParameter('p',1);
		$pagesize = $this->request->getGetParameter('ps',10);
		$sel['bt'] = $bt = $this->request->getGetParameter('bt');
		$sel['et'] = $et = $this->request->getGetParameter('et');
		$sel['user_name'] = $name = $this->request->getGetParameter('name');
		
		$allcordmsg = $this->getAllCordAndSmsNum($sel,$page,$pagesize);
		$tolNum = $this->getTolNum($sel);
		$this->page = $page;
		$this->pagesize = $pagesize;
		$this->begtime = $bt;
		$this->endtime = $et;
		$this->stats = $allcordmsg;
		$this->tolNum = $tolNum[0]['NUM'];
		$this->name = $name;
		return $this->render();
	}
	public function quesnr_statsAction() {
		$allquesnr = $this->getAllQuesnrInfo();
		$allAnswer = $this->getAllAnswerInfo();
		foreach ($allquesnr as $v){
			$quesnrs[$v['QID']] = $v['TITLE'];
		}
		unset($allquesnr);
		$this->allanswer = $allAnswer;
		$this->quesnrs = $quesnrs;
		return $this->render();
	}
	public function lookinfoAction() {
		$qid = $this->request->getGetParameter('id');
		$quesnr = $this->getQuesnrByIn($qid);
		$allselAnswer = $this->getAllSelAnswerByQid($qid);
		$alloptAnswer = $this->getAllOptAnswerByQid($qid);
		$allotherAnswer = $this->getAllOtherAnswerByQid($qid);
//		var_dump($allotherAnswer);
		$qids = $quesnr['Q_IDS'];
		$allQuest = $this->getAllTit('q_id in ('.$qids.')');
		$allOpt = $this->getAllOpt($qids);
		unset($qids);
		foreach ($allQuest as $v){
			$qids .= $v['Q_ID'].',';
			$quest[$v['Q_ID']]['QTYPE'] = $v['QTYPE'];
			$quest[$v['Q_ID']]['TITLE'] = $v['TITLE'];
			$quest[$v['Q_ID']]['ALLOW_NULL'] = $v['ALLOW_NULL'];
		}
		foreach ($allselAnswer as $v){
			if(!empty($v['OID'])){
				$selan[$v['OID']] = $v['NUMS'];
			}
		}
		foreach ($alloptAnswer as $v){
			$optval[$v['Q_ID']][] = $v;
		}
		foreach ($allotherAnswer as $v){
			$other[$v['OID']][] = $v;
		}
//		var_dump($other);
//		var_dump($quest);
		unset($allQuest);
		unset($allselAnswer);
		unset($alloptAnswer);
		unset($allotherAnswer);
		$qids = explode(',', rtrim($qids,','));
		$optarr = ATools::getConfig('app.qcat.type');
		$this->qids = $qids;
		$this->allAquest = $quest;
		$this->allOpt = $allOpt;
		$this->selan = $selan;
		$this->optval = $optval;
		$this->optarr = $optarr;
		$this->quesnr = $quesnr;
		$this->other = $other;
		return $this->render();
	}
	public function feed_statsAction() {
		$allFeed = $this->getAllFeedInfo();
		$allState = $this->getAllStateInfo();
		foreach ($allFeed as $v) {
			$feeds[$v['TYPE']] = $v['NUM'];
		}
		foreach ($allState as $v) {
			$allsates[$v['STATE']] = $v['NUM'];
		}
		unset($allFeed);
		unset($allState);
		$feedtype = ATools::getConfig('app.feedback.type');
		$feedstate = ATools::getConfig('app.feedback.state');
		
		$this->feeds = $feeds;
		$this->types = $feedtype;
		$this->states = $feedstate;
		$this->allstate = $allsates;
		return $this->render();
	}
	public function ask_statsAction() {
		$allDep = $this->getAllDep();
		$allhandle = $this->getAllHandleInfo();
		$allNothandle= $this->getAllNotHandleInfo();
		
		foreach ($allhandle as $v){
			$handle[$v['DNO']] = $v['NUM'];
		}
		foreach ($allNothandle as $v){
			$nothandle[$v['DNO']] = $v['NUM'];
		}
		foreach ($allDep as $v){
			if(!empty($handle[$v['DID']]) || !empty($nothandle[$v['DID']])){
				$data[$v['DID']]['name'] = $v['DN'];
				$data[$v['DID']]['handle'] = $handle[$v['DID']];
				$data[$v['DID']]['nothandle'] = $nothandle[$v['DID']];
			}
		}
		$this->data = $data;
		return $this->render();
	}
	
	function getAllQuesnrInfo(){
		if(CSM_UNIT_ID<0){
			$where = 'state>0';
		}else{
			$where = 'state>0 and unit_id='.$this->unid;
		}
		return JYDB::findAll('QUESTIONNAIRE',$where,'ques_id qid,title');
	}
	function getAllDep(){
		if($this->unid<0){
			$where = '1=1';
		}else{
			$where = 'unit_id='.$this->unid;
		}
		return JYDB::findAll('DEP',$where,'DEP_NO DID,DEP_NAME DN');
	}
	function getAllHandleInfo(){
		if($this->unid<0){
			$where = '1=1';
		}else{
			$where = 'unit_id='.$this->unid;
		}
		$where .= ' and state=2 ';
		$sql = "select state,count(*) num,dep_no dno from ASK where ".$where.' group by state,dep_no';
		return JYDB::query($sql);
	}
	function getAllNotHandleInfo(){
		if($this->unid<0){
			$where = '1=1';
		}else{
			$where = 'unit_id='.$this->unid;
		}
		$where .= ' and state<>2 ';
		$sql = "select state,count(*) num,dep_no dno from ASK where ".$where.' group by state,dep_no';
		return JYDB::query($sql);
	}
	function getAllAnswerInfo(){
		if(CSM_UNIT_ID<0){
			$where = 'state>0';
		}else{
			$where = 'state>0 and unit_id='.$this->unid;
		}
		$sql = "select ques_id qid,count(*) num from survey where ".$where.' group by ques_id';
		return JYDB::query($sql);
	}
	function getAllSelAnswerByQid($qid){
		$sql = "select opt_id oid, count(*) nums from survey_detl where ques_id=".$qid.' and opt_id>0 group by opt_id';
		return JYDB::query($sql);
	}
	function getAllOptAnswerByQid($qid){
		return JYDB::findAll('SURVEY_DETL','ques_id='.$qid.' and opt_id is null','Q_ID,opt_value optval');
	}
	function getAllOtherAnswerByQid($qid){
		return JYDB::findAll('SURVEY_DETL','ques_id='.$qid.' and opt_id is not null and opt_value is not null','opt_id oid,opt_value optval');
	}
	function getAllFeedInfo(){
		if(CSM_UNIT_ID<0){
			$where = 'state>0';
		}else{
			$where = 'state>0 and unit_id='.$this->unid;
		}
		$sql = "select type,count(*) num from feedback where ".$where.' group by type';
		return JYDB::query($sql);
	}
	function getAllStateInfo(){
		if(CSM_UNIT_ID<0){
			$where = 'state>0';
		}else{
			$where = 'state>0 and unit_id='.$this->unid;
		}
		$sql = "select state,count(*) num from feedback where ".$where.' group by state';
		return JYDB::query($sql);
	}
	function getAllCordAndSmsNum($sel,$page,$pagesize){
		if(CSM_UNIT_ID<0){
			$wherea = $whereb = $wherec = $whered = $wheree = 'where 1=1';
		}else{
			$wherea = $whereb = $wherec = $whered = $wheree = 'where unit_id='.$this->unid;
		}
		if(is_array($sel)){
			foreach ($sel as $k=>$v){
				if(!empty($v)){
					if($k=='bt'){
						$wherea .= ' and start_time>\''.$v.'\'';
						$whereb .= ' and send_time>\''.$v.'\'';
						$wherec .= ' and attend_time>\''.$v.'\'';
						$whered .= ' and visit_time>\''.$v.'\'';
						continue;
					}
					if($k=='et'){
						$wherea .= ' and start_time<\''.$v.'\'';
						$whereb .= ' and send_time<\''.$v.'\'';
						$wherec .= ' and attend_time<\''.$v.'\'';
						$whered .= ' and visit_time<\''.$v.'\'';
						continue;
					}
					if($k=='user_name'){
						$wherea .= ' and '.$k.' like \'%'.$v.'%\'';
						continue;
					}
				}
			}
		}
//		echo $whereb;
		$sql = 'select a.unid      usid,
				       e.doc_no    code,
				       a.user_name uname,
				       a.tol       toltime,
				       a.tnum      tnum,
				       b.snum      snum,
				       c.qnum      qnum,
				       d.vnum      vnum
				  from (select user_id unid,
				               user_name,
				               sum(DURATION) tol,
				               count(user_id) tnum
				          from telrecord '.$wherea.'
				         group by user_id, user_name) a
				  left join (select oper_user_id userid, count(*) snum from sms '.$whereb.' group by oper_user_id) b on a.unid = b.userid
				  left join (select user_id unid,count(*) QNUM from SURVEY '.$wherec.' group by user_id) c on c.unid  = a.unid
				  left join (select user_id unid,count(*) vnum from VISIT '.$whered.' group by user_id) d on d.unid=a.unid
				  left join sysuser e on e.user_id = a.unid';
		return JYDB::query($sql,$page,$pagesize);
	}
	function getTolNum($sel){
		if(CSM_UNIT_ID<0){
			$wherea = 'where 1=1';
		}else{
			$wherea = 'where unit_id='.$this->unid;
		}
		if(is_array($sel)){
			foreach ($sel as $k=>$v){
				if(!empty($v)){
					if($k=='bt'){
						$wherea .= ' and start_time>\''.$v.'\'';
						continue;
					}
					if($k=='et'){
						$wherea .= ' and start_time<\''.$v.'\'';
						continue;
					}
					if($k=='user_name'){
						$where .= ' and '.$k.' like \'%'.$v.'%\'';
						continue;
					}
				}
			}
		}
//		echo $whereb;
		$sql = 'select count(*) num
				  from (select user_id unid,
				               user_name,
				               sum(DURATION) tol,
				               count(user_id) tnum
				          from telrecord '.$wherea.'
				         group by user_id, user_name)
				         where 1=1 '.$where;
		return JYDB::query($sql);
	}
	function getQuesnrByIn($id){
		return JYDB::find('QUESTIONNAIRE','ques_id='.$id);
	}
	function getAllTit($w=''){ //返回医院的所有题目
		if($this->unid<0){
			$where = 'state=1 ';
		}else{
			$where = 'unit_id='.$this->unid.' and state=1 ';
		}
		
		if(!empty($w)){
			$where .= 'and '.$w;
		}
//		echo $where;
		return JYDB::findAll('QUESTION',$where,'allow_null,qtype,q_id,title,cat','QTYPE');
	}
	function getAllOpt($selId){
		$where = 'state=1 and q_id in ('.$selId.')';
		return JYDB::findAll('QUESTION_OPTIONS',$where,'opt_id oid,opt_name oname,allow_input input,q_id qid','allow_input desc,sort_no');
	}
}